package analyze;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Test {
	public static void main(String[] args) {
		String[] sSex = { "男", "女" };
		String[] sSize = { "4", "6", "8" };
		String[] sLifeHabit = { "默认", "安静", "热闹" };
		String[] sLiveHobby = { "规律", "不规律" };
//		choleric；乐观的：sanguine；冷漠的：phlegmatic；忧郁的：melancholic
		String[] sTemp = {"choleric","sanguine","phlegmatic","melancholic",
				"choleric-sanguine","choleric-phlegmatic","choleric-melancholic",
				"sanguine-phlegmatic","sanguine-melancholic","phlegmatic-melancholic",
				"choleric-sanguine-phlegmatic","choleric-sanguine-melancholic","sanguine-phlegmatic-melancholic",
				"choleric-sanguine-phlegmatic-melancholic"};
		ArrayList<String> temp = new ArrayList<String>();
//		四人间 学生 分组
		ArrayList<String[]> values4 = new ArrayList<String[]>();
//		六人间 学生 分组
		ArrayList<String[]> values6 = new ArrayList<String[]>();
//		八人间 学生 分组
		ArrayList<String[]> values8 = new ArrayList<String[]>();
//		由于房间不够统一分组,例如四人间一共选了7人值分配一间有3人无法入住.放男生
		ArrayList<String> values61 = new ArrayList<String>();
//		由于房间不够统一分组,例如四人间一共选了7人值分配一间有3人无法入住.放女生
		ArrayList<String> values62 = new ArrayList<String>();
		for (int i = 0; i < sSex.length; i++) {
			for (int j = 0; j < sSize.length; j++) {
				for (int j2 = 0; j2 < sLifeHabit.length; j2++) {
					for (int k = 0; k < sLiveHobby.length; k++) {
						temp = analyze(sSex[i], sSize[j], sLifeHabit[j2],sLiveHobby[k]);
						int a = temp.size();
//						System.out.println(sSex[i]+sSize[j]+sLifeHabit[j2]+sLiveHobby[k]);
						if (sSize[0].equals("4")) {
//							根据选四人间分配这么多寝室
							int group = a / 4;
//							余下的4人间同学数量
							int yu=a%4;
//							循环条件
							int m = 1;
//							将同学加入到集合列表中values4中,第一个元素为第一个寝室的4名同学.
							do {
								String ss[] = { 
										temp.get(4 * m-4),
										temp.get(4 * m-3), 
										temp.get(4 * m-2),
										temp.get(4 * m-1)};
								values4.add(ss);
								m++;
							}while (m < group);
//							由于4人间不够将余下同学统一分配到6人间.,例子共7人选4人间,余三人.不能入住
							if(yu==0){
//								没有人员空缺不处理,否则的话进行分寝
							}else{
								if(sSex[i].equals("男")){
									for (int l = 0; l < yu; l++) {
										values61.add(temp.get(group*4+l));
									}
								}else{
									for (int l = 0; l < yu; l++) {
										values62.add(temp.get(group*4+l));
									}
								}
							}						
						} else if (sSize[1].equals("6")) {
							int group = a / 6;
							int yu=a%6;
							int m = 1;
							do {
								String ss[] = { 
										temp.get(6 * m-6),
										temp.get(6 * m-5), 
										temp.get(6 * m-4),
										temp.get(6 * m-3),
										temp.get(6 * m-2),
										temp.get(6 * m-1)};
								values6.add(ss);
								m++;
							}while (m < group);
							if(yu==0){
//								没有人员空缺不处理,否则的话进行分寝
							}else{
								if(sSex[i].equals("男")){
									for (int l = 0; l < yu; l++) {
										values61.add(temp.get(group*4+l));
									}
								}else{
									for (int l = 0; l < yu; l++) {
										values62.add(temp.get(group*4+l));
									}
								}
							}	
						} else if (sSize[2].equals("8")) {
							int group = a / 8;
							int yu=a%8;
							int m = 1;
							do {
								String ss[] = { 
										temp.get(8 * m-8),
										temp.get(8 * m-7), 
										temp.get(8 * m-6), 
										temp.get(8 * m-5), 
										temp.get(8 * m-4), 
										temp.get(8 * m-3), 
										temp.get(8 * m-2),
										temp.get(8 * m-1)};
								values8.add(ss);
								m++;
							}while (m < group);
							if(yu==0){
//								没有人员空缺不处理,否则的话进行分寝
							}else{
								if(sSex[i].equals("男")){
									for (int l = 0; l < yu; l++) {
										values61.add(temp.get(group*4+l));
									}
								}else{
									for (int l = 0; l < yu; l++) {
										values62.add(temp.get(group*4+l));
									}
								}
							}
						}
					
					}
				}
			}
		}
	}
	private static ArrayList<String> analyze(String sex1, String size1,
			String lifeHabit1, String liveHobby1) {
		ArrayList<String> a = new ArrayList<String>();
		String sql = "select * from student";
		Connection conn = Conn.getConn();
		PreparedStatement pstmt;
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs;
			rs = pstmt.executeQuery();
			while (rs.next()) {
				// 学号
				String id = rs.getString(1);
				// 名字
				String name = rs.getString(3);
				// 性别
				String sex = rs.getString(4);
				// 地址
				String add = rs.getString(7);
				// 爱好 吃,音乐
				String hobby = rs.getString(8);
				// 房间大小;4,6,8
				String roomSize = rs.getString(9);
				// 习惯 吸烟,喝酒
				String habit = rs.getString(10);
				// 生活习惯 安静,热闹
				String lifeHabit = rs.getString(11);
				// 生活规律 熬夜,规律
				String liveHobby = rs.getString(12);
				// 得分
				String message = rs.getString(14);
				if (sex.equals(sex1) 
						&& roomSize.equals(size1)
						&& lifeHabit.equals(lifeHabit1)
						&& liveHobby.equals(liveHobby1)) {
					a.add(id);
//					System.out.println(id+sex1+size1+lifeHabit1+liveHobby1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return a;
	}
}